﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QL_Nha_Sach_Phuong_Nam.Quản_lý_sách.Quản_lý_thông_tin_sách
{
    public partial class FormQuanlythongtinsach : Form
    {
        private CXulythongtinsach xulyTTS;
        string ma = "";

        public FormQuanlythongtinsach()
        {
            InitializeComponent();
        }

        private void FormQuanlythongtinsach_Load(object sender, EventArgs e)
        {
            xulyTTS = new CXulythongtinsach();

            hienthidsSach(xulyTTS.getdsSach());
        }

        #region cac ham chuc nang
        private void hienthidsSach(List<Kết_nối.Sach> ds_sach)
        {
            livsach.Items.Clear();
            foreach (Kết_nối.Sach sach in ds_sach)
            {
                if (sach.SachStatus == "live")
                {
                    ListViewItem lvi = livsach.Items.Add(sach.SachNo);
                    lvi.SubItems.Add(sach.SachName);
                    lvi.SubItems.Add(sach.NhaXuatBan.NhaXuatBanName);
                    lvi.SubItems.Add(sach.SachNamXuatBan);
                    lvi.SubItems.Add(hienthiSachTacGia(sach));
                    lvi.SubItems.Add(hienthiSachTheLoai(sach));
                    lvi.SubItems.Add(sach.SachPrice.ToString());
                }
            }
        }
        private string hienthiSachTacGia(Kết_nối.Sach sach)
        {
            string s = "";
            foreach (Kết_nối.Sach_TacGia stg in sach.Sach_TacGias)
            {
                s += stg.TacGia.TacGiaName + " ";
            }
            return s;
        }
        private string hienthiSachTheLoai(Kết_nối.Sach sach)
        {
            string s = "";
            foreach (Kết_nối.Sach_TheLoai stl in sach.Sach_TheLoais)
            {
                s += stl.TheLoai.TheLoaiName + " ";
            }
            return s;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem lvi in livsach.SelectedItems)
            {
                string ma = lvi.SubItems[0].Text;
                xulyTTS.XoaSach(ma);
            }
            hienthidsSach(xulyTTS.getdsSach());
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            FormEditSach fedit = new FormEditSach(0,"");
            fedit.ShowDialog();
            hienthidsSach(xulyTTS.getdsSach());
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem lvi in livsach.SelectedItems)
            {
                ma = lvi.SubItems[0].Text;
            }
            FormEditSach fedit = new FormEditSach(1, ma);
            fedit.ShowDialog();
            hienthidsSach(xulyTTS.getdsSach());
        }

        private void button1_Click(object sender, EventArgs e)
        {
            hienthidsSach(xulyTTS.getdsSach());
        }

        #endregion

        

        
    }
}
